using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCamera : MonoBehaviour
{
    GameObject player;
    public GameObject follow;
    public GameObject look_at;
    // Start is called before the first frame update
    void Start()
    {
        player = transform.parent.gameObject;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Look(player);
        Follow(follow);

        Vector3 look_at_rot = look_at.transform.eulerAngles;
        Vector3 camera_rot = transform.eulerAngles;
        camera_rot.y = look_at_rot.y;
        transform.rotation = Quaternion.Euler(camera_rot);
    }

    void Look(GameObject gameObject)
    {
        Vector3 player_pos = gameObject.transform.position;
        Vector3 camera_pos = transform.position;
        float y = player_pos.y + 1.5f - camera_pos.y;
        float x = (float)Math.Pow(Math.Pow(player_pos.x - camera_pos.x, 2) + Math.Pow(player_pos.z - camera_pos.z, 2), 1.0f / 2);
        Quaternion quaternion = Quaternion.Euler(new Vector3((float)(-Math.Atan(y / x) / Math.PI * 180), 0, 0));
        transform.rotation = quaternion;
    }

    Vector3 last_pos = Vector3.zero;
    void Follow(GameObject gameObject)
    {
        Vector3 follow_pos = gameObject.transform.position;
        Vector3 now_pos = last_pos + (follow_pos - last_pos) * 0.08f;
        transform.position = now_pos;
        last_pos = now_pos;
    }
}
